package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.controller.DTO.TreeDTO;
import com.example.entity.Member;
import com.example.service.MemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

@RestController
/*@CrossOrigin*/

@RequestMapping("/member")
public class MemberControl {

    @Resource
    private MemberService memberService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Member member) {
        memberService.add(member);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        memberService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        memberService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Member member) {
        memberService.updateById(member);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Member member = memberService.selectById(id);
        return Result.success(member);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Member member) {
        List<Member> list = memberService.selectAll(member);
        return Result.success(list);
    }


    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Member member,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Member> page = memberService.selectPage(member, pageNum, pageSize);
        return Result.success(page);
    }

    // 获取人员树形结构
    @GetMapping("/treeJson")
    public Result treeJson() {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();

        List<Member> memberList = this.memberService.list(memberQueryWrapper);

        List<TreeDTO> dataList = new ArrayList<>();

        // 用for循环遍历membersList，将列表转换为树形数据
        for (Member member : memberList) {
            TreeDTO treeDTO = new TreeDTO();
            treeDTO.setId(member.getId());
            treeDTO.setParentId(member.getParentId());
            treeDTO.setMateId(member.getMateId());
            treeDTO.setName(member.getName());
            treeDTO.setImage_url(member.getAvatar());
            dataList.add(treeDTO);
        }
        // 构建树形结构
        List<TreeDTO> departmentList = new ArrayList<>();
        for (TreeDTO child : dataList) {
            if (child.getParentId() == 0) {
                departmentList.add(child);
            } else if (child.getParentId() != 0) {
                for (TreeDTO parent : dataList) {
                    if (child.getParentId().equals(parent.getId())) {
                        // 组合父子节点的关系
                        parent.getChildren().add(child);
                        for (TreeDTO mate : dataList) {
                            {
                                if (mate.getMateId() != 0) {
                                    if (mate.getMateId().equals(mate.getId())) {
                                        // 组合节点的关系
                                        mate.getMate().add(mate);
                                    }
                                }
                            }
                        }
                    }
                }

            } else if (child.getParentId() ==null){
                for (TreeDTO mate : dataList) {
                    {
                        if (mate.getMateId().equals(mate.getId())) {
                            // 组合节点的关系
                            mate.getMate().add(mate);
                        }

                    }
                }
            }



        }

            return Result.success(departmentList);
        }



}